package oboe.utils;

import flash.utils.getTimer;

import oboe.core.Controller;

import flash.Vector;

class Benchmark extends Controller
{
private var tests:Vector<Test>;
private var currentTest:Int;
private var maxCycles:Int;
private var currentCycle:Int;
private var done:Bool;

public function new( cycles:Int )
{
	super();
	
	currentCycle = 0;
	this.maxCycles = cycles;
	tests = new Vector();
	done = false;
}

public function addTest( name:String, func:Function ):Void
{
	tests.push( new Test(name, func) );
}

public function printResults():Void
{
	var min:Int = 0x7FFFFF;
	var acc:Int = 0;
	var test:Test;
	for each( test in this.tests )
	{
	min = Math.min( min, test.score );
	acc += test.score;
	}
	
	for each( test in this.tests )
	{
	trace(
		test.name, ": ",
		"\toverall %:", Math.round( (test.score / acc)*10000 )/100,
		"\t\trelative %:", Math.round( (test.score / min)*10000 )/100
		);
	}
}

public function slot_onTick( time:Float ):Void
{
	if( currentCycle >= maxCycles )
	{
	if( !done )	printResults();
	done = true;
	this.selfDestruct();
	return;
	}
	var begin:Int = getTimer();
	
	this.tests[ this.currentTest ].func();
	
	var end:Int = getTimer();
	this.tests[ currentTest ].score += end - begin;
	
	currentTest++;
	if( currentTest == tests.length )
	{
	currentTest = 0;
	currentCycle++;
	}
}
}
}

class Test
{
public var name:String;
public var score:Int;
public var func:Function;
public function Test( name:String, func:Function )
{
this.name = name;
this.func = func;
this.score = 0;
}